package com.ran.gpstestbed;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.ListFragment;

import java.util.ArrayList;
import java.util.Set;

public class DevicesFragment extends ListFragment {


    private BluetoothAdapter bluetoothAdapter;
    private final ArrayList<BluetoothDevice> listItems = new ArrayList<>();
    private ArrayAdapter<BluetoothDevice> listAdapter;

    public class DeviceListActivity extends AppCompatActivity {
        private ListView listPairedDevices, listAvailableDevices;
        private ProgressBar progressScanDevices;

        private ArrayAdapter<String> adapterPairedDevices, adapterAvailableDevices;
        private Context context;
        private BluetoothAdapter bluetoothAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.device_list_header);
            context = this;

            init();
        }

        private void init() {
            listPairedDevices = findViewById(R.id.list_paired_devices);
            listAvailableDevices = findViewById(R.id.list_available_devices);
            progressScanDevices = findViewById(R.id.progress_scan_devices);

            adapterPairedDevices = new ArrayAdapter<String>(context, R.layout.device_list_item);
            adapterAvailableDevices = new ArrayAdapter<String>(context, R.layout.device_list_item);

            listPairedDevices.setAdapter(adapterPairedDevices);
            listAvailableDevices.setAdapter(adapterAvailableDevices);

            listAvailableDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    String info = ((TextView) view).getText().toString();
                    String address = info.substring(info.length() - 17);

                    Intent intent = new Intent();
                    intent.putExtra("deviceAddress", address);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });

            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

            if (pairedDevices != null && pairedDevices.size() > 0) {
                for (BluetoothDevice device : pairedDevices) {
                    adapterPairedDevices.add(device.getName() + "\n" + device.getAddress());
                }
            }

            IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(bluetoothDeviceListener, intentFilter);
            IntentFilter intentFilter1 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            registerReceiver(bluetoothDeviceListener, intentFilter1);

            listPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Context context = getContext();
                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    bluetoothAdapter.cancelDiscovery();

                    String info = ((TextView) view).getText().toString();
                    String address = info.substring(info.length() - 17);

                    Log.d("Address", address);

                    Intent intent = new Intent();
                    intent.putExtra("deviceAddress", address);

                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }
            });
        }

        private BroadcastReceiver bluetoothDeviceListener = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    context = getContext();
                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        return;
                    }
                    if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                        adapterAvailableDevices.add(device.getName() + "\n" + device.getAddress());
                    }
                } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                    progressScanDevices.setVisibility(View.GONE);
                    if (adapterAvailableDevices.getCount() == 0) {
                        Toast.makeText(context, "No new devices found", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, "Click on the device to start the chat", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_device_list, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_scan_devices:
                    scanDevices();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

        private void scanDevices() {
            progressScanDevices.setVisibility(View.VISIBLE);
            adapterAvailableDevices.clear();
            Toast.makeText(context, "Scan started", Toast.LENGTH_SHORT).show();

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            if (bluetoothAdapter.isDiscovering()) {
                bluetoothAdapter.cancelDiscovery();
            }

            bluetoothAdapter.startDiscovery();
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();

            if (bluetoothDeviceListener != null) {
                unregisterReceiver(bluetoothDeviceListener);
            }
        }
    }}
